import os

import pandas as pd
from pandas import Series
from pandas.core.interchange.dataframe_protocol import DataFrame

from tools import get_files_name
from tools import BASE_PATH_5G实训
from tools import extract_parts_from_series
from tools import *


def get_实训报告_提交记录(output_path=None) -> DataFrame:
    df_学生_信息 = get_学生_信息()

    files = get_files_name(f"{BASE_PATH_5G实训}/学生提交", return_type="name_only")
    se_files_name = pd.Series(files, name="文件名")

    se_学号 = get_学号_segment(se_files_name)
    se_姓名 = get_姓名_segment(se_files_name)
    se_real_日期 = get_real_日期_segment(se_files_name)

    df_merged = pd.concat([se_学号, se_姓名, se_real_日期], axis=1)
    # combined = combine_series_to_dataframe(se_学号, se_姓名, se_real_日期)
    # combined['上机'] = 'Y'

    if output_path:
        df_merged.to_excel(output_path, index=False)
        print(f"\n✅ 实训报告提交记录统计完成! 文件已保存至: {output_path}\n")

    return df_merged

def calc_上机成绩() -> DataFrame:
    se_files_name = get_files_name(f"{BASE_PATH_5G实训}/学生提交")

    se_学号 = get_学号_segment(se_files_name).astype(int)
    se_姓名 = get_姓名_segment(se_files_name)
    se_real_日期 = get_real_日期_segment(se_files_name)
    se_logical_日期 = get_logical_日期_segment(se_files_name)

    se_logical_date = convert_mmdd_to_date(se_logical_日期)
    se_成绩 = calculate_date_scores(se_logical_date, 38)
    print(se_成绩)


def merge_student_scores(df_提交记录: DataFrame) -> DataFrame:
    df_学生信息 = get_学生_信息()
    try:
        df_merged = pd.merge(
            left=df_学生信息[['学号', '姓名']],
            right=df_提交记录[['学号', '提交日期']],
            on='学号',
            how='left'
        )

    except Exception as e:
        print(f"❌ 合并失败: {str(e)}")
        raise

    df_merged.to_excel(f"{BASE_PATH_5G实训}\导出成绩.xlsx", index=False)
    print(f"✅ 数据合并完成! 文件已保存至: {BASE_PATH_5G实训}\导出成绩.xlsx")

    return df_merged


if __name__ == "__main__":
    path1 = f"{BASE_PATH_5G实训}\学生提交\实训报告提交_汇总.xlsx"
    df_提交记录 = get_实训报告_提交记录(path1)
    # print(df_提交记录)

    calc_上机成绩()
    # df_merged = merge_student_scores(df_提交记录)
    # print(df_merged)
